/**
 * 
 * Opened Touchable RIA Framework - A Web RIA Javascript Framework
 * 
 * Copyright (c) 2012
 * Dual licensed under the MIT or GPL v2 licenses.
 * 
 * @author  CuTe_CuBe
 * @version $Id: client.js 8 2012-03-11 14:16:41Z cc.hgfeng@gmail.com $
 */

/**
 * OTR.Client 提供客户端平台检测方法
 * 
 */
OTR.Client = {

    /**
     * 平台标识
     */
    WINDOWS:       'Windows',
    LINUX:         'Linux',
    MAC:           'Macintosh',
    IPHONE:        'iPhone',
    IPAD:          'iPad',
    ANDROID:       'Android',
    WINDOWSPHONE:  'Windows Phone OS',
    WINDOWSMOBILE: 'Windows CE',
    BLACKBERRY:    'BlackBerry',
    SYMBIAN:       'SymbianOS',

    /**
     * 浏览器标识
     */
    MSIE:       'msie',
    MOZILLA:    'mozilla',
    WEBKIT:     'webkit',
    OPERA:      'opera',
    UNKNOWN:    'unknown',

    /**
     * 获取|匹配当前用户浏览器
     * 
     * @param  string | undefined compare
     * @return string | boolean
     */
    browser: function(compare) {
        if (undefined === this._browser) {
            this._matchUA();
        }
        
        if (undefined !== compare) {
            return compare == this._browser;
        }
        
        return this._browser;
    },

    /**
     * 获取|比较当前客户端版本号
     * 当浏览器为 unknow 或 不能获取时返回 null
     * 比较返回值见 OTR.Util.versionCompare
     * 
     * @param  string | undefined compare
     * @return string | boolean
     */
    version: function(compare) {
        if (undefined === this._version) {
            this._matchUA();
        }
        
        if (undefined !== compare) {
            return OTR.Util.versionCompare(compare, this._version);
        }
        
        return this._version;
    },

    /**
     * 获取客户端平台
     * 
     * @return string
     */
    platform: function(compare) {
        if (undefined === this._platform) {
            // 各平台标识
            var regs = [
                /(Windows)[\s]?[^Phone|CE]/,      // windows
                /(Linux)/,                        // linux
                /(Macintosh)/,                    // mac
                /(iPhone)/,                       // iphone
                /(iPad)/,                         // ipad
                /(Android)/,                      // android
                /(Windows[\s]?Phone[\s]?OS)/,     // windows phone
                /(Windows[\s]?CE)/,               // windows mobile/ce
                /(BlackBerry)/,                   // blackberry
                /(SymbianOS)/                     // symbian
            ], ua = this.ua();

            this._platform = this.UNKNOWN;
            for (var i = 0, c = regs.length; i < c; i++) {
                var match = null;
                if (null !== (match = regs[i].exec(ua))) {
                    this._platform = match[1];
                    break;
                }
            }
        }
        
        if (undefined !== compare) {
            return compare == this._platform;
        }
        
        return this._platform;
    },

    /**
     * 获取客户端userAgent
     * 
     * @return string
     */
    ua: function() {
        if (undefined === this._ua) {
            this._ua = navigator.userAgent;
        }

        return this._ua;
    },

    /**
     * 匹配userAgent内容
     * 
     * @return Array
     */
    _matchUA: function() {
        var regwk      = /(webkit)[\/]([\w.]+)/i,
            regopera   = /(opera)(?:.*version)?[\/]([\w.]+)/i,
            regmsie    = /(msie)[\s]?([\w.]+)/i,
            regmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/i,
            ua         = this.ua();

        var match = regmsie.exec(ua) || regwk.exec(ua)
                    || regmozilla.exec(ua) || regopera.exec(ua);
        
        if (null !== match) {
            this._browser = match[1].toLowerCase();
            this._version = match[2];

            return ;
        }
        
        this._browser = this.UNKNOWN;
        this._version = null;
    }
};